using System;
using System.Linq;
using UnityEditor;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay.Editors
{
    public static class AnimatorFactoryUtil
    {
        public static AnimationClip LoadAnimClip(string path)
        {
            return (AnimationClip)AssetDatabase.LoadAssetAtPath(path, typeof(AnimationClip));
        }

        public static AnimationClip LoadAnimClipAndSetLoopExtend(string path, string clipName, string entityName, bool loop, Func<string, ModelImporterClipAnimation, bool> importProcess)
        {
            var rClipName =  $"{entityName}@{clipName}";
            ModelImporter modelImporter = AssetImporter.GetAtPath(path) as ModelImporter;
            ModelImporterClipAnimation tempClip = null;
            ModelImporterClipAnimation[] modelImporterClips = null;
            if (modelImporter != null)
            {
                if (modelImporter.clipAnimations.Length > 0)
                {
                    modelImporterClips = modelImporter.clipAnimations;
                    // tempClip = modelImporterClips[0];
                }
                else if (modelImporter.defaultClipAnimations.Length > 0)
                {
                    modelImporterClips = modelImporter.defaultClipAnimations;
                    // tempClip = modelImporterClips[0];
                }
                for (int i = 0; i < modelImporterClips?.Length; i++)
                {
                    var t = modelImporterClips[i];
                    if (t.name == rClipName)
                    {
                        tempClip = t;
                        break;
                    }
                }

            }

            if (tempClip != null)
            {
                bool modify = false;
                if (tempClip.loopTime != loop)
                {
                    tempClip.loopTime = loop;
                    modify |= true;
                }

                if (importProcess != null)
                {
                    modify |= importProcess(clipName, tempClip);
                }

                if (modify)
                {
                    modelImporter.clipAnimations = modelImporterClips;
                    modelImporter.SaveAndReimport();
                }
            }
            var rAssets = AssetDatabase.LoadAllAssetsAtPath(path);
            foreach (var r in rAssets)
            {
                if (r is AnimationClip clip && r.name == rClipName)
                {
                    return clip;
                }
            }
            return (AnimationClip)AssetDatabase.LoadAssetAtPath(path, typeof(AnimationClip));
        }


        public static AnimationClip LoadAnimClipAndSetLoop(string path, bool loop)
        {
            ModelImporter modelImporter = AssetImporter.GetAtPath(path) as ModelImporter;
            if (modelImporter != null && modelImporter.clipAnimations.Length > 0)
            {
                var clipAnimations = modelImporter.clipAnimations;
                ModelImporterClipAnimation tempClip = clipAnimations[0];
                if (tempClip != null && tempClip.loopTime != loop)
                {
                    tempClip.loopTime = loop;
                    modelImporter.clipAnimations = clipAnimations;
                    modelImporter.SaveAndReimport();
                }
            }
            else if (modelImporter != null && modelImporter.defaultClipAnimations.Length > 0)
            {
                var clipAnimations = modelImporter.defaultClipAnimations;
                ModelImporterClipAnimation tempClip = clipAnimations[0];
                if (tempClip != null && tempClip.loopTime != loop)
                {
                    tempClip.loopTime = loop;
                    modelImporter.clipAnimations = clipAnimations;
                    modelImporter.SaveAndReimport();
                }
            }

            return (AnimationClip)AssetDatabase.LoadAssetAtPath(path, typeof(AnimationClip));
        }

        public static bool GetAnimClipLoopState(string path)
        {
            ModelImporter modelImporter = AssetImporter.GetAtPath(path) as ModelImporter;
            if (modelImporter != null && modelImporter.clipAnimations.Length > 0)
            {
                var clipAnimations = modelImporter.clipAnimations;
                ModelImporterClipAnimation tempClip = clipAnimations[0];
                if (tempClip != null)
                    return tempClip.loopTime;
            }
            else if (modelImporter != null && modelImporter.defaultClipAnimations.Length > 0)
            {
                var clipAnimations = modelImporter.defaultClipAnimations;
                ModelImporterClipAnimation tempClip = clipAnimations[0];
                if (tempClip != null)
                    return tempClip.loopTime;
            }
            return false;
        }

        public static AnimationClip LoadAnimClip(string fbxPath, string animPath)
        {
            var objs = AssetDatabase.LoadAllAssetsAtPath(fbxPath);
            return objs.Where(o => o is AnimationClip && o.name.Equals(animPath)).Select(o => o as AnimationClip).FirstOrDefault();
        }
    }

}